package com.evil.common.rocketmq.entity;

import com.evil.common.core.enums.BaseEnum;
import com.evil.common.core.exception.BusinessException;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import lombok.AllArgsConstructor;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.BufferedReader;
import java.io.IOException;
import java.security.Principal;
import java.util.*;

/**
 * @author liyang
 * @date 2023-05-12 14:44
 */
public class MqRequestAttributes extends ServletRequestAttributes {
    public MqRequestAttributes(MqHttpServletRequest request) {
        super(request);
    }

    public static MqRequestAttributes build(String sessionId, Map<String, String> attributeMap) {
        return new MqRequestAttributes(new MqHttpServletRequest(sessionId, attributeMap));
    }

    @AllArgsConstructor
    public static class MqHttpServletRequest implements HttpServletRequest {

        private final String sessionId;

        private final Map<String, String> attributeMap;

        @Override
        public String getAuthType() {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public Cookie[] getCookies() {
            return null;
        }

        @Override
        public long getDateHeader(String name) {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public String getHeader(String name) {
            return this.attributeMap.get(name);
        }

        @Override
        public Enumeration<String> getHeaders(String name) {
            final int[] i = {0};
            String value = this.attributeMap.get(name);
            return new Enumeration<>() {
                @Override
                public boolean hasMoreElements() {
                    return i[0] == 0;
                }

                @Override
                public String nextElement() {
                    i[0]++;
                    return value;
                }
            };
        }

        @Override
        public Enumeration<String> getHeaderNames() {
            ArrayList<String> keys = new ArrayList<>(this.attributeMap.keySet());
            final int[] i = {0};
            return new Enumeration<String>() {
                @Override
                public boolean hasMoreElements() {
                    return i[0] < keys.size() - 1;
                }

                @Override
                public String nextElement() {
                    i[0]++;
                    return keys.get(i[0]);
                }
            };
        }

        @Override
        public int getIntHeader(String name) {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public String getMethod() {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public String getPathInfo() {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public String getPathTranslated() {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public String getContextPath() {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public String getQueryString() {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public String getRemoteUser() {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public boolean isUserInRole(String role) {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public Principal getUserPrincipal() {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public String getRequestedSessionId() {
            return this.sessionId;
        }

        @Override
        public String getRequestURI() {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public StringBuffer getRequestURL() {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public String getServletPath() {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public HttpSession getSession(boolean create) {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public HttpSession getSession() {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public String changeSessionId() {
            throw new BusinessException(BaseEnum.RUN_ERROR);
        }

        @Override
        public boolean isRequestedSessionIdValid() {
            return false;
        }

        @Override
        public boolean isRequestedSessionIdFromCookie() {
            return false;
        }

        @Override
        public boolean isRequestedSessionIdFromURL() {
            return false;
        }

        @Override
        public boolean authenticate(HttpServletResponse response) {
            return false;
        }

        @Override
        public void login(String username, String password) {
        }

        @Override
        public void logout() {
        }

        @Override
        public Collection<Part> getParts() {
            return null;
        }

        @Override
        public Part getPart(String name) {
            return null;
        }

        @Override
        public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) {
            return null;
        }

        @Override
        public Object getAttribute(String name) {
            return this.attributeMap.get(name);
        }

        @Override
        public Enumeration<String> getAttributeNames() {
            ArrayList<String> keys = new ArrayList<>(this.attributeMap.keySet());
            final int[] i = {0};
            return new Enumeration<String>() {
                @Override
                public boolean hasMoreElements() {
                    return i[0] < keys.size() - 1;
                }

                @Override
                public String nextElement() {
                    i[0]++;
                    return keys.get(i[0]);
                }
            };
        }

        @Override
        public String getCharacterEncoding() {
            return null;
        }

        @Override
        public void setCharacterEncoding(String env) {

        }

        @Override
        public int getContentLength() {
            return 0;
        }

        @Override
        public long getContentLengthLong() {
            return 0;
        }

        @Override
        public String getContentType() {
            return null;
        }

        @Override
        public ServletInputStream getInputStream() throws IOException {
            return null;
        }

        @Override
        public String getParameter(String name) {
            return null;
        }

        @Override
        public Enumeration<String> getParameterNames() {
            return null;
        }

        @Override
        public String[] getParameterValues(String name) {
            return new String[0];
        }

        @Override
        public Map<String, String[]> getParameterMap() {
            return null;
        }

        @Override
        public String getProtocol() {
            return null;
        }

        @Override
        public String getScheme() {
            return null;
        }

        @Override
        public String getServerName() {
            return null;
        }

        @Override
        public int getServerPort() {
            return 0;
        }

        @Override
        public BufferedReader getReader() throws IOException {
            return null;
        }

        @Override
        public String getRemoteAddr() {
            return null;
        }

        @Override
        public String getRemoteHost() {
            return null;
        }

        @Override
        public void setAttribute(String name, Object o) {

        }

        @Override
        public void removeAttribute(String name) {

        }

        @Override
        public Locale getLocale() {
            return null;
        }

        @Override
        public Enumeration<Locale> getLocales() {
            return null;
        }

        @Override
        public boolean isSecure() {
            return false;
        }

        @Override
        public RequestDispatcher getRequestDispatcher(String path) {
            return null;
        }

        @Override
        public int getRemotePort() {
            return 0;
        }

        @Override
        public String getLocalName() {
            return null;
        }

        @Override
        public String getLocalAddr() {
            return null;
        }

        @Override
        public int getLocalPort() {
            return 0;
        }

        @Override
        public ServletContext getServletContext() {
            return null;
        }

        @Override
        public AsyncContext startAsync() throws IllegalStateException {
            return null;
        }

        @Override
        public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException {
            return null;
        }

        @Override
        public boolean isAsyncStarted() {
            return false;
        }

        @Override
        public boolean isAsyncSupported() {
            return false;
        }

        @Override
        public AsyncContext getAsyncContext() {
            return null;
        }

        @Override
        public DispatcherType getDispatcherType() {
            return null;
        }

        @Override
        public String getRequestId() {
            return null;
        }

        @Override
        public String getProtocolRequestId() {
            return null;
        }

        @Override
        public ServletConnection getServletConnection() {
            return null;
        }
    }
}
